package com.zilla.android.lib.ui.setting;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.github.snowdream.android.util.Log;
import com.zilla.android.lib.ui.R;

/**
 * 设置布局
 * User: zilla
 * Date: 13-10-26
 * Time: 下午12:00
 */
public class SettingLayout extends LinearLayout {

    private LayoutInflater mInflater;
    private Context mContext;
    private int item_top = 0;
    private int item_bottom = 0;
    private int item_middle = 0;
    private int item_single = 0;

    public SettingLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    public SettingLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView(context, attrs);
    }

    /**
     * 初始化控件
     *
     * @param context
     */
    private void initView(Context context, AttributeSet attrs) {
        this.mInflater = LayoutInflater.from(context);
        this.mContext = context;
        this.setOrientation(LinearLayout.VERTICAL);
//        mInflater.inflate(R.layout.ui_setting_layout, this, true);

        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemLayout);

        item_top = typedArray.getResourceId(R.styleable.SettingLayout_settingLayout_item_top, R.drawable.setting_top);
        item_bottom = typedArray.getResourceId(R.styleable.SettingLayout_settingLayout_item_bottom, R.drawable.setting_bottom);
        item_middle = typedArray.getResourceId(R.styleable.SettingLayout_settingLayout_item_middle, R.drawable.setting_middle);
        item_single = typedArray.getResourceId(R.styleable.SettingLayout_settingLayout_item_single, R.drawable.setting_single);

        int bg = typedArray.getResourceId(R.styleable.SettingLayout_settingLayout_bg, R.drawable.transparent);
        if (bg != R.drawable.transparent) {
            this.setBackgroundResource(bg);
        }
        typedArray.recycle();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        initChild();
    }

    private void initChild() {
        int count = this.getChildCount();
        if (count == 0) return;
        if (count == 1) {
            SettingItemLayout itemLayout = (SettingItemLayout) getChildAt(0);
            if (itemLayout.getTag() == null) {
                itemLayout.setBackgroundResource(item_single);
            }
            return;
        }
        for (int i = 0; i < count; i++) {
            SettingItemLayout itemLayout = (SettingItemLayout) getChildAt(i);
            if (itemLayout.getTag() != null) continue;
            if (i == 0) {
                itemLayout.setBackgroundResource(item_top);
            } else if (i == count - 1) {
                itemLayout.setBackgroundResource(item_bottom);
            } else {
                itemLayout.setBackgroundResource(item_middle);
            }
        }
    }
}
